package com.sskj.one;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.multidex.MultiDexApplication;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hjq.toast.ToastUtils;
import com.hjq.toast.style.ToastQQStyle;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.sskj.common.BuildConfig;
import com.sskj.common.router.service.CommonService;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.LanguageUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.SPConfig;
import com.sskj.lib.router.service.LibService;
import com.zxy.recovery.core.Recovery;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Locale;

import cn.jpush.android.api.JPushInterface;


public class MyApplication extends MultiDexApplication {
    @Autowired
    CommonService commonService;
    @Autowired
    LibService libService;

    @Override
    public void onCreate() {
        super.onCreate();
        closeAndroidPDialog();
        initBackgroundCallBack();

//        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
//        StrictMode.setVmPolicy(builder.build());
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//            builder.detectFileUriExposure();
//        }


        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
        if (BuildConfig.DEBUG) {
            ARouter.openDebug();
//            Recovery.getInstance()
//                    .debug(true)
//                    .recoverInBackground(false)
//                    .recoverStack(true)
//                    .mainPage(MainActivity.class)
//                    .recoverEnabled(true)
//                    .callback(null)
//                    .silent(false, Recovery.SilentMode.RECOVER_ACTIVITY_STACK)
//                    .init(this);
        }
        ARouter.init(this);
        ARouter.getInstance().inject(this);
        commonService = new CommonService();
        commonService.initOkGoHttp(this, BuildConfig.DEBUG);
        ToastUtils.init(this);
        ToastUtils.initStyle(new ToastQQStyle());
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> new ClassicsHeader(context));
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> new ClassicsFooter(context));

        if (LanguageUtil.getAppLanguage(this).equals("zh")) {
            LanguageUtil.changeAppLanguage(this, Locale.SIMPLIFIED_CHINESE, false);
        } else if (LanguageUtil.getAppLanguage(this).equals("en")) {
            LanguageUtil.changeAppLanguage(this, Locale.ENGLISH, false);
        }

    }

    private void closeAndroidPDialog() {
        try {
            Class aClass = Class.forName("android.content.pm.PackageParser$Package");
            Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class);
            declaredConstructor.setAccessible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Class cls = Class.forName("android.app.ActivityThread");
            Method declaredMethod = cls.getDeclaredMethod("currentActivityThread");
            declaredMethod.setAccessible(true);
            Object activityThread = declaredMethod.invoke(null);
            Field mHiddenApiWarningShown = cls.getDeclaredField("mHiddenApiWarningShown");
            mHiddenApiWarningShown.setAccessible(true);
            mHiddenApiWarningShown.setBoolean(activityThread, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getToken() {
        return SPUtil.get(SPConfig.TOKEN, "");
    }

    public String getAccount() {

        return SPUtil.get(SPConfig.ACCOUNT, "");
    }

    private int appCount;
    private boolean isRunInBackground;


    private void initBackgroundCallBack() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {

            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {
                appCount++;
                if (isRunInBackground) {
                    isRunInBackground = false;
                    //应用从后台回到前台 需要做的操作
                    RxBus.getDefault().send(RxBusCode.REFRESH_HANGQING);
                }

            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {
                appCount--;
                if (appCount == 0) {
                    //应用进入后台 需要做的操作
                    isRunInBackground = true;
                }

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
    }


}
